package net.bioteam.perl5;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;

public class Perl5ScriptTest
{

	/**
	 * @param args
	 * @throws ScriptException 
	 */
	public static void main(String[] args) throws ScriptException
	{
		Map<String, String> cmdlines = new HashMap<String, String>();
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("perl");
		//output script to stdout.
		Perl5ScriptEngine.SCRIPT_DEBUG=true;

		if (engine != null)
		{
			ScriptContext newContext = new SimpleScriptContext();
			Bindings values = newContext.getBindings(ScriptContext.ENGINE_SCOPE);


			//input value is number
			values.put("a", new Integer(5));
			values.put("b", new Integer(6));
			Object obj=engine.eval("$a+$b", newContext);
			System.out.println("11=|"+obj.toString()+"|");
			
			
			
			//input value is string
			values.clear();
			values.put("test1", "test");
			values.put("test2", "test");
			obj=engine.eval("$test1.$test2", newContext);
			//string concatenation.
			System.out.println("testtest=|"+obj.toString()+"|");
			
			//regular expression: a true value.
			obj=engine.eval("$test1=~/est/", newContext);
			System.out.println("1=|"+obj.toString()+"|");
			
			//regular expression: demo how false value is returned.
			obj=engine.eval("$test1=~/xxx/", newContext);
			System.out.println("EMPTY=|"+obj.toString()+"|");

			//Integer list
			values.clear();
			ArrayList<Integer> list=new ArrayList<Integer>();
			list.add(1);
			list.add(2);
			list.add(3);
			values.put("list", list);
			obj=engine.eval("my $sum=0; for my $tmp (@list){$sum+=$tmp;} $sum;", newContext);
			System.out.println("6=|"+obj.toString()+"|");
			
			//string list
			values.clear();
			ArrayList<String> strs=new ArrayList<String>();
			strs.add("str1");
			strs.add("str2");
			strs.add("str3");
			values.put("list", strs);
			obj=engine.eval("$list[1];", newContext);
			System.out.println("str2=|"+obj.toString()+"|");
			
			//Integer map
			values.clear();
			Map<Integer, Integer> map=new HashMap<Integer, Integer>();
			map.put(1, 1);
			map.put(2, 2);
			map.put(2, 2);
			values.put("map", map);
			obj=engine.eval("$map{2};", newContext);
			System.out.println("2=|"+obj.toString()+"|");
			
			//string map
			values.clear();
			Map<Integer, String> maps=new HashMap<Integer, String>();
			maps.put(1, "str1");
			maps.put(2, "str2");
			maps.put(3, "str3");
			values.put("map", maps);
			obj=engine.eval("$map{3};", newContext);
			System.out.println("str3=|"+obj.toString()+"|");
			
			
			//integer equal
			values.clear();
			values.put("inx", "1");
			values.put("value", "1");
			values.put("str", "str");
			values.put("test", "test String");
			obj=engine.eval("$inx==1?\" is 1\":\"not 1\";\n", newContext);
			System.out.println("is 1=|"+obj.toString()+"|");
			
			obj=engine.eval("$test=~/test/;", newContext);
			System.out.println("true boolean return as:1=|"+obj.toString()+"|");
			
			obj=engine.eval("$test=~/txxx/;", newContext);
			System.out.println("false boolean return as:EMPTY=|"+obj.toString()+"|");
			
			obj=engine.eval("$value==5;", newContext);
			System.out.println("false is return as:EMPTY=|"+obj.toString()+"|");
			
			obj=engine.eval("$value==1;", newContext);
			System.out.println("true is return as:1|"+obj.toString()+"|");
			
			
			obj=engine.eval("$str eq 'str';", newContext);
			System.out.println("true is return as |"+obj.toString()+"|");
			
			obj=engine.eval("$str eq 'str1';", newContext);
			System.out.println("false is return as |"+obj.toString()+"|");
			
			//how to return as array.
			values.clear();
			values.put("str", "1 2 3 4 5 6 7");
			obj=engine.eval("my @_tmp=split(/\\s/, $str)", newContext);
			System.out.println("array is return as |"+obj.toString()+"|");
		} else
		{
			System.out.println("No perl engine is found");
		}

	}

}
